home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
DELPHI32
/
BOXES
/
THSTCBO2
/
HSTCBO.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-05-26
|
5KB
|
177 lines
unit Hstcbo;
{******************************************************************************
THistoryComboBox:
FREEWARE !
Version 1.5, May 96
Author: Christian Holzner, Austria
Internet: cholzner@ping.at
********************************************************************************
Reads/Writes Items of a TComboBox to/from Registry.
The reading and writing has to be initiated manually (methods: ReadRegistry,
WriteRegistry). It could also done in Create/Destroy but then I would loose
flexibility.
If you find any bugs or enhance this component I would like to get an info.
********************************************************************************
Added properties to TComboBox
Key: Key in Registry
If left blank then no Registry-access is made
(= it behaves like a normal ComboBox)
FMaxHistoryLength: Maxmimum allowed length of the history-items
Added methods to TComboBox
ReadRegistry: Reads items from Registry
WriteRegistry: Writes items to Registry
AddToList: Adding an item to the Historylistbox
*******************************************************************************
History:
1.0, Jan. 96: Uses Ini-file for storage.
1.5, May 96 : Uses Registry for storage
Replaced 'Exit' with 'Break' which caused memory leak. (My
Clipper history affected me with the 'Exit' statement ;-)
New method 'AddToList' by Jeff Goldberg (jeffg@haven.ios.com)
*******************************************************************************}
interface
uses
SysUtils, Windows, Classes, StdCtrls, Forms, Registry;
type
THistoryComboBox = class(TComboBox)
private
FKey: String;
FMaxHistoryLength: Integer;
protected
{ Protected-Deklarationen }
public
constructor Create(AOwner: TComponent); Override;
procedure ReadRegistry;
procedure WriteRegistry;
procedure AddToList(NewItem: String);
published
property Key: String
read FKey write FKey;
property MaxHistoryLength: Integer
read FMaxHistoryLength write FMaxHistoryLength
default 7;
end;
procedure Register;
implementation
constructor THistoryComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMaxHistoryLength := 7;
end;
procedure THistoryComboBox.ReadRegistry;
{ Reads items from Registry }
var
Registry: TRegistry;
i : Integer;
FileName, Value: String;
begin
if FKey <> '' then begin
{ Default Rootkey is 'HKey_Current_User\Software' }
Registry := TRegistry.Create;
try
Clear;
{ Read maximum allowed entries from Registry }
Registry.RootKey := HKey_Current_User;
if Registry.OpenKey('\Software\'+FKey,False) then begin
for i:= 0 to FMaxHistoryLength-1 do begin
Value := Registry.ReadString('H'+IntToStr(i));
If Value <> ''
then Items.Add(Value)
else break;
end;
end;
finally
Registry.Free;
end;
end;
end;
procedure THistoryComboBox.WriteRegistry;
{ Writes items to registry }
var
Registry: TRegistry;
FileName: String;
i : Integer;
begin
if FKey <> '' then begin
{ Default Rootkey is 'HKey_Current_User\Software' }
Registry := TRegistry.Create;
try
{ Read maximum allowed entries from Registry }
Registry.RootKey := HKey_Current_User;
if Registry.OpenKey('\Software\'+FKey,False) then begin
try
for i:= 0 to FMaxHistoryLength-1 do
Registry.ReadString('H'+IntToStr(i));
except
on ERegistryException do;
end;
end;
{ Erase existing entries to get rid of more then the max. allowed entries }
if Registry.OpenKey('\Software',False) then
Registry.DeleteKey('FKey');
{ Add to Historylistbox }
AddToList(Text);
{ Write to Registry }
if Registry.OpenKey('\Software\'+FKey,True) then
for i := 0 to Items.Count-1 do
Registry.WriteString('H'+IntToStr(i),Items[i]);
finally
Registry.Free;
end;
end;
end;
procedure THistoryComboBox.AddToList(NewItem: String);
{ Add a string to the Historylistbox }
var
i: integer;
begin
if NewItem <> '' then begin
{ Insert in first position }
Items.Insert(0,NewItem);
{ Check maximum numer of entries and delete any duplicate }
for i := 1 to Items.Count-1 do
if (Items[i] = NewItem) or (i > FMaxHistoryLength-1) then
Items.Delete(i);
end;
end;
procedure Register;
begin
RegisterComponents('Standard',[THistoryComboBox]);
end;
end.